package com.dy.net.mail;

import android.nfc.Tag;
import android.util.Log;

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.event.TransportListener;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/*
 *****************************************
 *项目名称:GetTestPoint
 *@Author jzb
 *创建时间 20220104 18:51
 *
 *****************************************
 */
public class MailTool {
    private static String TAG = "MailTool";
    public static void sendEmail(String toAddress,File file, TransportListener transportListener) throws Exception {
        Log.e(TAG, "sendEmail: file"+ file.getAbsolutePath());
        // 收件人电子邮箱
        //String to = "1163558472@qq.com";
        String to = toAddress;

        // 发件人电子邮箱
        String from = "1580685639@qq.com";

        // 获取系统属性
        Properties properties = new Properties();
        // 设置邮件服务器
        properties.setProperty("mail.transport.protocol", "smtp");
        properties.setProperty("mail.smtp.host", "smtp.qq.com");
        properties.setProperty("mail.smtp.port", "587");
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.timeout", "1000");
        properties.put("mail.debug", true);

        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties,
                new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        // 登陆邮件发送服务器的用户名和密码
                        return new PasswordAuthentication(
                                "1580685639@qq.com", "gkybhliacrcdjgah");
                    }
                });

        MimeBodyPart text = new MimeBodyPart();
        text.setContent("<h4>测试文件！</h4>", "text/html;charset=UTF-8");

        //创建邮件附件
        MimeBodyPart attach = new MimeBodyPart();
        DataHandler dh = new DataHandler(new FileDataSource(file));
        attach.setDataHandler(dh);
        attach.setFileName(dh.getName());

        // 创建默认的 MimeMessage 对象
        MimeMessage message = new MimeMessage(session);

        // Set From: 头部头字段
        InternetAddress address = new InternetAddress(from);
        message.setFrom(address);

        InternetAddress ccAddress = new InternetAddress("2772507694@qq.com");
        message.addRecipient(Message.RecipientType.CC, ccAddress);

        // Set To: 头部头字段
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        //创建容器描述数据关系
        MimeMultipart mp = new MimeMultipart();
        mp.addBodyPart(text);
        mp.addBodyPart(attach);
        mp.setSubType("mixed");

        message.setSubject("deyi");
        message.setContent(mp);
        message.saveChanges();

        Transport transport = session.getTransport();
        transport.connect();
        transport.addTransportListener(transportListener);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }


}
